package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.megaprosoft.modyimcarcare.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CheckCarByHedActivity extends Activity {
	Button bth,btnB,edit;
	TextView RegID,time;
	RadioButton chx1,chx2;
	connect con = new connect(); 
	ResultSet rs;
	//private boolean input=false;
	ArrayList<String> Status = new ArrayList<String>();	
	public static String DTIDS,ID;
	String StatuS,ServiceID,pass,RegisID,id,Time,Type,NameUser,Normal1="";
	
	
	String NameInput,company;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_carcare_check_car);
		
		
		NameInput = getIntent().getExtras().getString("name");
		company = getIntent().getExtras().getString("CompanyS");
		
        
			RegID = (TextView) findViewById(R.id.ShowRegID);		
			bth = (Button) findViewById(R.id.button1);
			//btd = (Button) findViewById(R.id.delete);
			edit = (Button) findViewById(R.id.edit);
			chx1 = (RadioButton) findViewById(R.id.C1);		
			chx2 = (RadioButton) findViewById(R.id.C2);	
			
			
			id = getIntent().getExtras().getString("IntentID");		
			RegisID = getIntent().getExtras().getString("RegisID");	
			Type = getIntent().getExtras().getString("Type");
			NameUser = getIntent().getExtras().getString("nameCheck");
			RegID.setText(RegisID); 
	
					Status();
					delete();
	///////////////////////////time /////////////////////////////				
					Calendar c = Calendar.getInstance();
					SimpleDateFormat df = new SimpleDateFormat("HH:mm dd-MM-yyyy");
					String formattedDate = df.format(c.getTime());

			        // txtResult
			        time = (TextView) findViewById(R.id.TimeviewList);
			        time.setText(formattedDate);
			        Time = time.getText().toString();
			        
			        
					///////////////////////////////////////////////////1		
			        chx1.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
					// TODO Auto-generated method stub
						chx1.isChecked();
					Normal1 ="���º����";
					}
					});
					
					
					///////////////////////////////////////////////////////2		
			        chx2.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
					// TODO Auto-generated method stub
						chx2.isChecked();
					Normal1 ="������º����";
					}
					});
			        
			        

//=========================================EDIT==========================================================================
			        
			        edit.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {	
							
					      String sql = "SELECT getCar.id AS getCarID "+
						      ",getCar.ServiceID AS ServiceID "+
						      ",getCar.RegisterID AS RegisterID "+
						      ",DataService.TypeID AS TypeID "+
						      ",CustomerCar.CarGroup AS CarGroup "+
						  
						  "FROM getCar "+ 
						  
						  "LEFT JOIN DataService "+
						  "ON getCar.ServiceID = DataService.ServiceID "+
						  
						  "LEFT JOIN CustomerCar "+
						  "ON getCar.ServiceID = CustomerCar.ServiceID "+
						  
						  "WHERE getCar.ServiceID = "+id+" ";
					      
			String 	getCarID = "";	  
			String 	ServiceID = "";
			String 	RegisterID = "";
			String 	getCarIDtoEdit = "";
			String  CarGroupID = "";
					  	rs = con.selectData(sql);		   
						try {
							//while(rs.next()){ 
							    rs.next();
							    getCarID = rs.getString("getCarID"); 
								ServiceID = rs.getString("ServiceID"); 
								RegisterID = rs.getString("RegisterID"); 
								getCarIDtoEdit = rs.getString("TypeID"); 
								CarGroupID = rs.getString("CarGroup"); 
								
							if(getCarIDtoEdit.equals("1")){	
								Intent goCheck = new Intent(CheckCarByHedActivity.this,services_edit.class);
							    goCheck.putExtra("getCarID", getCarID);
								goCheck.putExtra("ServiceID", ServiceID);
								goCheck.putExtra("RegisterID", RegisterID);
								goCheck.putExtra("TypeID", getCarIDtoEdit);
								goCheck.putExtra("CarGroupID", CarGroupID);
								goCheck.putExtra("name", NameInput);
								goCheck.putExtra("CompanyS", company);
								goCheck.putExtra("IntentID", id);
								goCheck.putExtra("addSer", "");
								startActivity(goCheck);
							}	else{
								Intent goCheck = new Intent(CheckCarByHedActivity.this,HoleActivity_edit.class);
							    goCheck.putExtra("getCarID", getCarID);
								goCheck.putExtra("ServiceID", ServiceID);
								goCheck.putExtra("RegisterID", RegisterID);
								goCheck.putExtra("TypeID", getCarIDtoEdit);
								goCheck.putExtra("CarGroupID", CarGroupID);
								goCheck.putExtra("name", NameInput);
								goCheck.putExtra("CompanyS", company);
								goCheck.putExtra("IntentID", id);
								goCheck.putExtra("addHole", "");
								startActivity(goCheck);
							}
								
							//}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							Toast.makeText(CheckCarByHedActivity.this, id , Toast.LENGTH_LONG).show();
						}    
						 
							
							
			
					
		}
	});				
					
////////////////////////////////Button	ok///////////////////////////	
				bth.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
				

				String sql = "UPDATE ServiceDetail_old SET Status = '"+StatuS+"',LastUpdate='"+Time+
				"' WHERE ServiceID = '"+id+"' " 	;
				
				
				
				if(con.editData(sql)){	 
				Toast.makeText(CheckCarByHedActivity.this, "����¹�ŧʶҹ�����", Toast.LENGTH_SHORT).show();
				Intent newActivityAdd = new Intent(CheckCarByHedActivity.this,Detail.class);
				newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
				startActivity(newActivityAdd);
				}else{
				Toast.makeText(CheckCarByHedActivity.this, "ʶҹ��ѧ�������¹�ŧ", Toast.LENGTH_SHORT).show();
				Intent newActivityAdd = new Intent(CheckCarByHedActivity.this,Detail.class);
				newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
				startActivity(newActivityAdd);
				}
				
				
				
				String sqlInsert = "INSERT INTO CheckByLeader " +
						"(ServiceID,ServiceType,RegisterID,namePersonnel,CheckCar,Status,timeCheck) " +
						"VALUES (" +
						"'"+id+"'" + ',' + 
						"'"+Type+"'" + ',' + 
						"'"+RegisID+"'" + ',' + 
						"'"+NameUser+"'" + ',' +
						"'"+Normal1+"'" + ',' +
						"'"+StatuS+"'" + ',' +
						"'"+Time+"'" + ") ";
				
				if(con.addData(sqlInsert)){	 
					
					Toast.makeText(CheckCarByHedActivity.this, "����¹�ŧʶҹ�����", Toast.LENGTH_SHORT).show();
					
					}else{
					Toast.makeText(CheckCarByHedActivity.this, "ʶҹ��ѧ�������¹�ŧ", Toast.LENGTH_SHORT).show();
					
					}
				}
				});
////////////////////////////////Button	cancel///////////////////////////	
				  // Current Date
				
				
	}

	
	///////////////////////////////////////// Status  ////////////////////////////
	
	private void Status() {
		
		String tusTmp = getIntent().getExtras().getString("Status");
		
		final Spinner spn = (Spinner) findViewById(R.id.dropstatus);

		
		Status.add(tusTmp);
		Status.add("��Ǩ�ͺ�������º����");
		Status.add("��º��������´���º����");
		Status.add("��Ǩ�����");
		
		
		
	ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Status);
	empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
	spn.setAdapter(empolyeeAdapter);

	spn.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

	public void onItemSelected(AdapterView<?> adapterView, 
		View view, int i, long l) { 
		// TODO Auto-generated method stub
		
		
		StatuS = Status.get(i);
		
		}
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		Toast.makeText(CheckCarByHedActivity.this,
					String.valueOf("ʶҹ��ա������¹�ŧ����"),
						Toast.LENGTH_SHORT).show();
	} 

	});

	}
	
	
	////////////////////////////////////////////////////////////////////
	
	@Override
	public void onBackPressed() {
	  
	        new AlertDialog.Builder(this)
	        .setTitle("¡��ԡ��÷���¡��")
	        .setMessage("��Ѻ�˹����ѡ")
	                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                        Intent newActivity = new Intent(getApplicationContext(),MainActivity.class);	  
	                        newActivity.putExtra("name", NameInput);
	        				newActivity.putExtra("CompanyS", company);
	        				newActivity.putExtra("Status", StatuS);
	                        startActivity(newActivity);
	                        finish();
	                        overridePendingTransition(R.layout.fadeendout,R.layout.fadeendin);
	                    }
	                })
	                .setPositiveButton(android.R.string.no, null).create().show();
	    

	}
	
	 private	void delete(){
		 final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
			final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
			final Button btd = (Button) findViewById(R.id.a_getCar_bt_general);
			
			btd.setOnClickListener(new View.OnClickListener() {
		        public void onClick(View v) {
		        	
		            final View Viewlayout = inflater.inflate(R.layout.blacklist,(ViewGroup) findViewById(R.id.layout_dialog));                
					
		            popDialog.setIcon(android.R.drawable.btn_star_big_on);	
		    		popDialog.setTitle("��س�����˵ؼŷ��¡��ԡ��ԡ��");
		            popDialog.setView(Viewlayout);
		            
             
             // Button OK
             popDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){

                 public void onClick(DialogInterface dialog, int which) {
                     dialog.dismiss();
                     
                    // Username and Password (Dialog)
                     EditText bl = (EditText) Viewlayout.findViewById(R.id.txtBL);
              
                    
                     String Reason = bl.getText().toString();
									
							String sqlInsertBlackList =  "INSERT INTO BlackList " +
									"(ServiceID,ServiceType,RegisterID,namePersonnel,Status,timeCheck,Reason) " +
									"VALUES (" +
									"'"+id+"'" + ',' + 
									"'"+Type+"'" + ',' + 
									"'"+RegisID+"'" + ',' + 
									"'"+NameUser+"'" + ',' +
									"'"+StatuS+"'" + ',' +
									"'"+Time+"'" + ',' +
									"'"+Reason+"'" + ") ";
							
							Toast.makeText(CheckCarByHedActivity.this,"����"+Reason,
		               	 		     Toast.LENGTH_LONG).show(); 
							
							
							if(con.addData(sqlInsertBlackList)){	 
								
								Toast.makeText(CheckCarByHedActivity.this, "save ����", Toast.LENGTH_SHORT).show();
								
								
								String sqlDELETE = "DELETE FROM ServiceDetail_old WHERE ServiceID = '"+id+"' "; 	
										
									if(con.addData(sqlDELETE))	
									{
									Intent goCheck = new Intent(CheckCarByHedActivity.this,Detail.class);
									goCheck.putExtra("name", NameInput);
									goCheck.putExtra("CompanyS", company);
									startActivity(goCheck);
									
									finish();
									
									Toast.makeText(CheckCarByHedActivity.this,"¡��ԡ��ԡ������"+id,
				               	 		     Toast.LENGTH_LONG).show(); 
										
									}else{
										Toast.makeText(CheckCarByHedActivity.this,"�ѧ�������������¹�ŧ",
					               	 		     Toast.LENGTH_LONG).show(); 
										
									}
								
								
								
								}else{
								Toast.makeText(CheckCarByHedActivity.this, "ʶҹ��ѧ�������¹�ŧ", Toast.LENGTH_SHORT).show();
								
								}		
									
			
								dialog.dismiss();
								 
								
								}
							})
		    	  
		    	  
				             .setNegativeButton("Cancel",
									  new DialogInterface.OnClickListener() {
									    public void onClick(DialogInterface dialog,int id) {
										dialog.cancel();
									    }
									  });
				
				           popDialog.create();
				           popDialog.show(); 
				
			}
		})   ;
     
     

	 }
	////////////////////////////////////////////////////////////////////
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
